<template>
   <div class="AdminHome">
    <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '#' }">首页</el-breadcrumb-item>
        </el-breadcrumb>
    <header>
        <div class="module">
            <span>{{ adminState.normal }}</span>
            <p>
                正常次数
            </p>
        </div>
        <div class="module">
            <span>{{ adminState.warning }}</span>
            <p>
                警告次数          
            </p>
        </div>
        <div class="module">
            <span>{{ adminState.error }}</span>
            <p>
                报警次数
            </p>
        </div>
        <div class="module">
            <span>{{ lookNum }}</span>
            <p>访问人数</p>
        </div>
        <div class="module">
            <span>{{ linkDevice }}</span>
            <p>
                设备数量
            </p>
        </div>
    </header>
    <el-container>
        <el-main>
            <el-row :gutter="20">
                <el-col :span="16" class="left">
                    <!-- 历史数据统计 -->
                    <div class="history">
                        <h1 style="font-size: 20px;height: 30px;">传感器状态统计</h1>
                        <div ref="deviceStatus" class="echart"></div>
                    </div>
                </el-col>
                <el-col :span="8" class="right">
                    <!-- 设备的分布 -->
                    <div class="right-pie">
                        <h1>传感器分布</h1>
                        <div ref="deviceRate" class="echart"></div>
                    </div>
                    <!-- 设备的报警状况 -->
                    <div class="right-status">
                        <h1>{{currentName}}的运行状态</h1>
                        <div ref="historyRef" class="echart"></div>
                    </div>
                </el-col>
           </el-row>
        </el-main>
    </el-container>

   </div>
</template>

<script setup lang="ts">
    import tool from '../../until/tool'
    import {ref,onMounted, reactive, getCurrentInstance} from 'vue'
    import * as echarts from 'echarts'
    import {amdinHistoryOption,adminDeviceOption,adminDeviceStatusOption} from '../../until/chart'
    import { IhttpReturn } from '../../until/interface' 
    import { adminStore } from '../../store/admin'
    
    import { storeToRefs } from 'pinia'
    let store = adminStore()
    let {lookNum,linkDevice} = storeToRefs(store)  
    let {proxy} = getCurrentInstance()
    let historyRef = ref()
    let deviceRate = ref()
    let deviceStatus = ref()
    let hisoryEchart = reactive(null) // 历史曲线
    let deviceEchart = reactive(null) // 设备的分布曲线
    let deviceStatusEchart = reactive(null) // 设备的详细状态
    let currentName = ref<string>('紫外') // 当前设备的名称
    let currentId = ref<number>(1)
        let adminState = reactive({
        normal: 0,
        error: 0,
        warning: 0
    })
    const deviceList:Array<{id:number,name:string}> = [
        {
            id: 1,
            name: "紫外"
        }, {
            id: 2,
            name: "红外"
        },
        {
            id: 3,
            name: "温度"
        }, {
            id: 4,
            name: "湿度"
        }, {
            id: 5,
            name: "压力"
        }
    ]
    // 初始化历史曲线
    let historyPirture = async () => {
        let {data:res}:{data:IhttpReturn} = await proxy.$adminHttp("/admin/getHistory?id=" + currentId.value)
        hisoryEchart.setOption(amdinHistoryOption(res.data))
    }
    // 平台传感器的分布
    let deviceRatePirture = () => {
        deviceEchart.setOption(adminDeviceOption())
    }

    //状态总理统计
    let deviceStatusPirture = async () => {
        let {data:res}:{data:IhttpReturn} = await proxy.$adminHttp("/admin/getHistory?type=all")
        let arr1 = res.data.map(item => item.normal) // 获取正常
        let arr2 = res.data.map(item => item.warning) // 获取警告
        let arr3 = res.data.map(item => item.error) // 获取错误
        let xdata = res.data.map(item => item.date) // 获取日期
        adminState.normal = tool.tool.sumArr(arr1)
        adminState.warning = tool.tool.sumArr(arr2)
        adminState.error = tool.tool.sumArr(arr3)
        deviceStatusEchart.setOption(adminDeviceStatusOption(xdata,arr1,arr2,arr3))
    }
    onMounted(() => {
        hisoryEchart = echarts.init(historyRef.value) // 初始化历史曲线
        deviceEchart = echarts.init(deviceRate.value) // 设备的分布
        deviceStatusEchart = echarts.init(deviceStatus.value) // 传感器的详情状态
        historyPirture()
        deviceRatePirture()
        deviceStatusPirture()
        // 适配
        window.addEventListener("resize",() => {
            setTimeout(() => {
                hisoryEchart.resize()
                deviceEchart.resize()
                deviceStatusEchart.resize()
            }, 500);
        })
        // 切换不同传感器的状态
        deviceEchart.on('click',(parmas) => {
            currentName.value = parmas.data.name 
            let id:number = deviceList.find(item => item.name === currentName.value).id
            currentId.value = id
            proxy.notifySuccess("切换到" + currentName.value)
            historyPirture()
        })
    })

</script>

<style lang="less" scoped>
.right {
    display: flex!important;
    flex-direction: column;
    h1 {
        font-size: 20px;
        height: 30px;
        line-height: 30px;
    }
    .echart {
        flex: 1;
    }
    .right-pie {
        flex: 1;
        display: flex;
        flex-direction: column;
    }
    .right-status {
        flex: 1;
        display: flex;
        flex-direction: column;
    }
}
    .el-container {
        height: calc(100% - 140px);
        .el-main {
            height: 100%;
        }
        .el-row {
            height: 100%;
            .history {
                height: 100%;
                .echart {
                    height: calc(100% - 30px);
                }
            }
        }
    } 
    .AdminHome {
        height: 100%;
        header {
            height: 120px;
            display: flex;
            margin-top: 10px;
            justify-content: space-between;
            .module {
                border-radius: 10px;
                width: 19%;
                box-shadow: 2px 1px 4px rgba(0,0,0,.4);
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                color: #fff;
                span {
                    font-size: 30px;
                }
                p {
                    font-size: 18px;
                }
                &:nth-child(1) {
                    background-color: #7759fb;
                }
                &:nth-child(2) {
                    background-color: #fe7b33;
                }
                &:nth-child(3) {
                    background-color: #fd5e61;
                }
                &:nth-child(4) {
                    background-color: #148df9;
                }
                &:nth-child(5) {
                    background-color: #2ed573;
                }
            }    
        }
    }
</style>